package org.example.SlApi.DzhyApi;

import io.restassured.RestAssured;
import io.restassured.response.Response;
import org.apache.commons.codec.net.URLCodec;
import org.example.JycInfo;
import org.example.utils.ReadConf;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import static org.example.CookieHandlingFilter.cookies;
import static org.example.RestClient.givenWithCookies;
import static org.example.RestClient.loginAndUpdateCookies;
import static org.example.SlApi.JbxxApi.JbxxbcApi.initJyclist;

public class DzhyApi {//多证合一保存
    public static void main(String[] args) throws IOException {
        dzhyApi();
    }
    private static List<JycInfo> JycList = new ArrayList<>();

    public static int dzhyApi() throws IOException {
        // 登录并更新cookies

        loginAndUpdateCookies();

        RestAssured.baseURI = ReadConf.getHost(); // 设置基础URI
        List<JycInfo> jycInfoList = initJyclist(); // 调用方法来获取列表
        if (!jycInfoList.isEmpty()) {
            JycInfo firstJycInfo = jycInfoList.get(0); // 获取列表中的第一个元素
            String pripid = firstJycInfo.getPripid(); // 使用实际的getter方法
            String sbid = firstJycInfo.getSbid();
            String entname = firstJycInfo.getEntname();
            String qylxdl = firstJycInfo.getQylxdl();
            String nameid = firstJycInfo.getName_id();


            URLCodec urlCodec = new URLCodec();
            String encodedParam = URLEncoder.encode(entname, "UTF-8");

            String data = "ywtSldj.do?method=edit&pripid="+pripid+"&sbid="+sbid+"&select_mkid=14&reslut=&qylxdl=11&csrftoken=";
            Response response = givenWithCookies()

                    .header("Content-Type","application/x-www-form-urlencoded")
//               .cookies(cookies)
//                    .body(data)
                    .log().all()
                    .get(data);
            int statusCode = Integer.parseInt(String.valueOf(response.getStatusCode()));
            System.out.println(statusCode);
            String body = response.getBody().asString();
            System.out.println(body);
            if (statusCode == 302) {

                String redirectUrl = response.getHeader("Location");
                System.out.println(redirectUrl);
                if (redirectUrl != null) {
                    Response finalResponse = givenWithCookies()
                            .log().all()
                            .when()
                            .get(redirectUrl);
                    int statusCode1 = finalResponse.getStatusCode();
                    System.out.println("Status Code: " + statusCode1);


                    return 0;

                }
                return 0;
            }}
        return 0;
    }
}
